/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Ad_RapInternalFrame.java
 *
 * Created on 04-04-2012, 22:42:08
 */
package GUI;

import DAO.MySQLDataAccessHelper;
import DAO.PhimDAO;
import DAO.PhongChieuDAO;
import POJO.PhimPoJo;
import POJO.PhongChieuPoJo;
import java.sql.ResultSet;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author tran quoc thinh
 */
public class Ad_RapInternalFrame extends javax.swing.JInternalFrame {

    /** Creates new form Ad_RapInternalFrame */
    public Ad_RapInternalFrame() {
        initComponents();
    }
    private int a =0;
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jtbDanhSachPhong = new javax.swing.JTable();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jtfTenPhong = new javax.swing.JTextField();
        jtfTongSoGhe = new javax.swing.JTextField();
        jbtnCapNhat = new javax.swing.JButton();
        jbtnXoa = new javax.swing.JButton();
        jbtnThem = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        jtfMaPhong = new javax.swing.JTextField();

        setClosable(true);
        setIconifiable(true);
        setMaximizable(true);
        setTitle("Chỉnh Sửa Phòng");
        addInternalFrameListener(new javax.swing.event.InternalFrameListener() {
            public void internalFrameActivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosed(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosing(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeactivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeiconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameIconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameOpened(javax.swing.event.InternalFrameEvent evt) {
                formInternalFrameOpened(evt);
            }
        });

        jtbDanhSachPhong.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        jtbDanhSachPhong.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jtbDanhSachPhongMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jtbDanhSachPhong);

        jPanel1.setBackground(new java.awt.Color(204, 204, 204));

        jLabel1.setText("Cập Nhật  Phòng");

        jLabel2.setText("Tên Phòng:");

        jLabel3.setText("Tổng Số Ghế:");

        jbtnCapNhat.setText("Cập Nhật");
        jbtnCapNhat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnCapNhatActionPerformed(evt);
            }
        });

        jbtnXoa.setText("Xóa");
        jbtnXoa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnXoaActionPerformed(evt);
            }
        });

        jbtnThem.setText("Thêm");
        jbtnThem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnThemActionPerformed(evt);
            }
        });

        jLabel5.setText("Mã Phòng:");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jLabel1)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(83, 83, 83)
                                .addComponent(jbtnThem, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 36, Short.MAX_VALUE)
                                .addComponent(jbtnXoa, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(12, 12, 12)))
                        .addGap(18, 18, 18)
                        .addComponent(jbtnCapNhat))
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addComponent(jLabel3)
                            .addGap(18, 18, 18)
                            .addComponent(jtfTongSoGhe))
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel5)
                                .addComponent(jLabel2))
                            .addGap(26, 26, 26)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(jtfMaPhong)
                                .addComponent(jtfTenPhong, javax.swing.GroupLayout.DEFAULT_SIZE, 206, Short.MAX_VALUE)))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(9, 9, 9)
                        .addComponent(jLabel5))
                    .addComponent(jtfMaPhong, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jtfTenPhong, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jtfTongSoGhe, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jbtnThem)
                    .addComponent(jbtnXoa)
                    .addComponent(jbtnCapNhat))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 336, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 166, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 166, Short.MAX_VALUE))
                .addGap(69, 69, 69))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formInternalFrameOpened(javax.swing.event.InternalFrameEvent evt) {//GEN-FIRST:event_formInternalFrameOpened
        // TODO add your handling code here:
        LoadPhong();    
    
    }//GEN-LAST:event_formInternalFrameOpened

    private void jtbDanhSachPhongMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jtbDanhSachPhongMouseClicked
        // TODO add your handling code here:
        a=this.jtbDanhSachPhong.getSelectedRow();
        this.jtfMaPhong.setText(this.jtbDanhSachPhong.getValueAt(a, 0).toString());
        this.jtfTenPhong.setText(this.jtbDanhSachPhong.getValueAt(a, 1).toString());
        this.jtfTongSoGhe.setText(this.jtbDanhSachPhong.getValueAt(a, 2).toString());
        
    }//GEN-LAST:event_jtbDanhSachPhongMouseClicked

    private void jbtnThemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnThemActionPerformed
        // TODO add your handling code here:
        String maphong=this.jtfMaPhong.getText().trim();
        String tenphong=this.jtfTenPhong.getText().trim();       
        int tongsoghe=Integer.valueOf(this.jtfTongSoGhe.getText().trim());     
            
      
        PhongChieuPoJo p=new PhongChieuPoJo();
        p.setMaPhong(maphong);
        p.setTenPhong(tenphong);
        p.setTongSoVe(tongsoghe);   
        
        int n=PhongChieuDAO.ThemPhong(p);
        if(n==1){
            JOptionPane.showMessageDialog(null,"Thêm Phong","Thanh Cong",JOptionPane.INFORMATION_MESSAGE);
            LoadPhong();
        }else{
             JOptionPane.showMessageDialog(null,"Thêm Phong","Khong Thanh Cong",JOptionPane.INFORMATION_MESSAGE);
        }     
    }//GEN-LAST:event_jbtnThemActionPerformed

    private void jbtnXoaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnXoaActionPerformed
        // TODO add your handling code here:
         if(a==-1){
             JOptionPane.showMessageDialog(null,"Thông Báo","Chưa Chọn",JOptionPane.INFORMATION_MESSAGE);
        }else{
            String maphong=this.jtbDanhSachPhong.getValueAt(a, 0).toString();
            int n=PhongChieuDAO.XoaPhong(maphong);
            if(n==1){
                JOptionPane.showMessageDialog(null,"Xóa Phong","Thanh Cong",JOptionPane.INFORMATION_MESSAGE);
                LoadPhong();
            }else{
                 JOptionPane.showMessageDialog(null,"Xóa Phong","Khong Thanh Cong",JOptionPane.INFORMATION_MESSAGE);
            }      
        }
    }//GEN-LAST:event_jbtnXoaActionPerformed

    private void jbtnCapNhatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnCapNhatActionPerformed
        // TODO add your handling code here:
        String maphong=this.jtfMaPhong.getText().trim();
        String tenphong=this.jtfTenPhong.getText().trim();       
        int tongsoghe=Integer.valueOf(this.jtfTongSoGhe.getText().trim());     
            
              
        PhongChieuPoJo p=new PhongChieuPoJo();
        p.setMaPhong(maphong);
        p.setTenPhong(tenphong);
        p.setTongSoVe(tongsoghe);       
        
        int n=PhongChieuDAO.SuaPhong(p);
        if(n==1){
            JOptionPane.showMessageDialog(null,"Cập Nhật Phòng","Thanh Cong",JOptionPane.INFORMATION_MESSAGE);
            LoadPhong();
        }else{
             JOptionPane.showMessageDialog(null,"Cập Nhật Phòng","Khong Thanh Cong",JOptionPane.INFORMATION_MESSAGE);
        }     
    }//GEN-LAST:event_jbtnCapNhatActionPerformed
  private void LoadPhong(){
       try{         
           ResultSet rs=PhongChieuDAO.LayBangPhongChieu();
           DefaultTableModel model =new DefaultTableModel(); 
           model.addColumn("MaPhong");
           model.addColumn("TenPhong");
           model.addColumn("SoGhe");                 
           while(rs.next()){
               Object[] a={rs.getString("MaPhong"),rs.getString("TenPhong"),rs.getString("TongSoGhe")};
               model.addRow(a);
           }
           this.jtbDanhSachPhong.setModel(model);        
        }catch(Exception ex){
            System.err.println(ex);
        }       
        MySQLDataAccessHelper helper=new MySQLDataAccessHelper();
        helper.Close();
  }
 
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton jbtnCapNhat;
    private javax.swing.JButton jbtnThem;
    private javax.swing.JButton jbtnXoa;
    private javax.swing.JTable jtbDanhSachPhong;
    private javax.swing.JTextField jtfMaPhong;
    private javax.swing.JTextField jtfTenPhong;
    private javax.swing.JTextField jtfTongSoGhe;
    // End of variables declaration//GEN-END:variables
}
